﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;

namespace moveErrorFile.classes
{
    class clsExcelHelper
    {
        public static void createFolderFileDetExcel(out string sErrorMsg, List<clsOrderConfigHeader> configList)
        {

            clsLogHelper.writeLog(out sErrorMsg, "createFolderFileDetExcel Start", true);
            sErrorMsg = null;
            DirectoryInfo dir = null;
            List<FileInfo> todayFiles = new List<FileInfo>();
            clsFileHelper fileHelp = new clsFileHelper();
            var workbook = new HSSFWorkbook();
            string sFileName = "";

            try
            {
                for (int i = 0; i < configList.Count(); i++)
                {
                    workbook = new HSSFWorkbook();
                    
                    clsOrderConfigHeader configHeader = configList[i];
                    sFileName = configHeader.SStorer + ".xls";

                    for (int j = 0; j < configHeader.getNoOfConfigLine(); j++)
                    {
                        clsOrderConfigLine configLine = configHeader.getOrderConfigLineByID(j);

                        if (configLine.SCheckFolder == "YES")
                        {
                            if(!string.IsNullOrEmpty(configLine.SOriginalDir))
                            {
                                //get original folder infor
                                dir = new DirectoryInfo(@configLine.SOriginalDir);
                                todayFiles = clsFileHelper.GetTodayFileInDirectory(dir);

                                //for original folder
                                var originSheet = workbook.CreateSheet(configHeader.SStorer + " - " + configLine.SOrderType + " - Origin");

                                //create worksheet header
                                var originRowHeader = originSheet.CreateRow(0);
                                originRowHeader.CreateCell(0).SetCellValue("FileName");
                                originRowHeader.CreateCell(1).SetCellValue("Creation Date");

                                for (int k = 0; k < todayFiles.Count(); k++)
                                {
                                    //create worksheet header
                                    var originRowDet = originSheet.CreateRow(k + 1);
                                    originRowDet.CreateCell(0).SetCellValue(todayFiles[k].Name);
                                    originRowDet.CreateCell(1).SetCellValue(todayFiles[k].CreationTime.ToLongDateString() + " " + todayFiles[k].CreationTime.ToLongTimeString());
                                }
                            }

                            //check for error folder
                            if (!string.IsNullOrEmpty(configLine.SErrorDir))
                            {
                                //for Error folder
                                clsLogHelper.writeLog(out sErrorMsg, "Checking folder : " + configLine.SErrorDir, true);
                                dir = new DirectoryInfo(@configLine.SErrorDir);
                                todayFiles = clsFileHelper.GetTodayFileInDirectory(dir);

                                var errorSheet = workbook.CreateSheet(configHeader.SStorer + " - " + configLine.SOrderType + " - Error");

                                //create worksheet header
                                var errorRowHeader = errorSheet.CreateRow(0);
                                errorRowHeader.CreateCell(0).SetCellValue("FileName");
                                errorRowHeader.CreateCell(1).SetCellValue("Creation Date");

                                for (int k = 0; k < todayFiles.Count(); k++)
                                {
                                    //check file list
                                    clsLogHelper.writeLog(out sErrorMsg, "  Checking file : " + todayFiles[k].ToString(), true);
                                    
                                    //create worksheet header
                                    var errorRowDet = errorSheet.CreateRow(k + 1);
                                    errorRowDet.CreateCell(0).SetCellValue(todayFiles[k].Name);
                                    errorRowDet.CreateCell(1).SetCellValue(todayFiles[k].CreationTime.ToLongDateString() + " " + todayFiles[k].CreationTime.ToLongTimeString());

                                    TimeSpan span = DateTime.Now - todayFiles[k].CreationTime;
                                    double totalMinutes = span.TotalMinutes;

                                    if (@configLine.SMoveErrorFileInd == "YES" && totalMinutes < @configLine.IMoveErrorFileTimeFrame)
                                    {
                                        string sFromLoc = @configLine.SErrorDir + @"\" + todayFiles[k].Name;
                                        string sToLoc = @configLine.SMoveErrorFileDir + @"\" + todayFiles[k].Name;

                                        clsLogHelper.writeLog(out sErrorMsg, "Move File " + sFromLoc + " -> " + sToLoc, true);
                                        clsFileHelper.MoveErrorFile(out sErrorMsg, sFromLoc , sToLoc);

                                        if (!string.IsNullOrEmpty(sErrorMsg))
                                        {
                                            clsLogHelper.writeLog(out sErrorMsg, "  Move file Error : " + sErrorMsg, true);
                                        }
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(configLine.SSuccessDir))
                            {
                                //for Success folder
                                dir = new DirectoryInfo(@configLine.SSuccessDir);
                                todayFiles = clsFileHelper.GetTodayFileInDirectory(dir);
                                var successSheet = workbook.CreateSheet(configHeader.SStorer + " - " + configLine.SOrderType + " - Success");

                                //create worksheet header
                                var successRowHeader = successSheet.CreateRow(0);
                                successRowHeader.CreateCell(0).SetCellValue("FileName");
                                successRowHeader.CreateCell(1).SetCellValue("Creation Date");

                                for (int k = 0; k < todayFiles.Count(); k++)
                                {
                                    //create worksheet header
                                    var successRowDet = successSheet.CreateRow(k + 1);
                                    successRowDet.CreateCell(0).SetCellValue(todayFiles[k].Name);
                                    successRowDet.CreateCell(1).SetCellValue(todayFiles[k].CreationTime.ToLongDateString() + " " + todayFiles[k].CreationTime.ToLongTimeString());
                                }
                            }

                            //Write the stream data of workbook to the root directory
                            using (var fileData = new FileStream(sFileName, FileMode.Create))
                            {
                                workbook.Write(fileData);
                            }

                        }

                        if (configLine.SCheckFolder == "YES")
                        {
                            string sTitle = "EDI Details for : " + configHeader.SStorer;
                            string sBody = "EDI Details for : " + configHeader.SStorer;

                            clsEmailHelper.sendEmail(out sErrorMsg, configHeader.SEmailListTo, sTitle, sBody, @sFileName);
                        }
                    }

                }

                clsLogHelper.writeLog(out sErrorMsg, "createFolderFileDetExcel End", true);
            }
            catch (Exception ex)
            {
                sErrorMsg = ex.Message;
            }
        }

    }
}
